<?php
namespace App\Model;

use DB;

class NewActivity{

	public $scool;

	public function __construct()
	{
		$this->scool = DB::connection('scool');
	}

	//获取所有 event
	public function getList($userId, $isAdmin)
	{
        if($isAdmin){
            return $this->scool->table('new_activity')
                ->select(DB::raw('count(new_activity_order.id) as countOrder, new_activity.*'))
                ->leftJoin('new_activity_order', 'new_activity_order.activity_id', '=', 'new_activity.id')
//                ->where('new_activity.user_id', $userId)
                ->groupBy('new_activity.id')
                ->orderBy('new_activity.id' ,'desc')
                ->paginate(10);
        }else{
            return $this->scool->table('new_activity')
                ->select(DB::raw('count(new_activity_order.id) as countOrder, new_activity.*'))
                ->leftJoin('new_activity_order', 'new_activity_order.activity_id', '=', DB::raw('new_activity.id AND new_activity_order.status = 1'))
                ->where('new_activity.user_id', $userId)
                ->groupBy('new_activity.id')
                ->orderBy('new_activity.id' ,'desc')
                ->paginate(10);
        }
	}

    //获取所有临时 event
    public function getListTmp($userId, $isAdmin)
    {
        if($isAdmin){
            return $this->scool->table('new_activity_tmp')
                ->where('status', 0)
                ->orderBy('id' ,'desc')
                ->paginate(10);
        }else{
            return $this->scool->table('new_activity_tmp')
                ->where('status', 0)
                ->where('user_id', $userId)
                ->orderBy('id' ,'desc')
                ->paginate(10);
        }

    }

	//创建数据
	public function create($data)
	{
		$ret = DB::connection('scool')->table('new_activity_tmp')->insertGetId($data);
        return $ret;
	}

    /**
     * 创建活动
     * @param $data
     * @return mixed
     */
    public function createEvent($data)
    {
        $ret = DB::connection('scool')->table('new_activity')->insertGetId($data);
        return $ret;
    }

    /**
     * 创建到 new_all_list 表中
     * @param $data
     * @return mixed
     */
    public function createAllList($data)
    {
        $ret = $this->scool->table('new_all_list')->insert($data);
        return $ret;
    }

	//更新数据
	public function update($data, $id)
	{
		return $this->scool->table('new_activity')->where('id', $id)->update($data);
	}

    //更新数据
    public function updatetmp($data, $id)
    {
        return $this->scool->table('new_activity_tmp')->where('id', $id)->update($data);
    }


	//获取单条数据信息
	public function getInfoById($id)
	{
		return $this->scool->table('new_activity')->where('id', $id)->first();
	}

    //获取单条数据信息
    public function getInfoByIdTmp($id, $userId)
    {
        return $this->scool->table('new_activity_tmp')->where('id', $id)->where('user_id', $userId)->first();
    }

    /**
     * 管理员获取未审核的活动信息
     * @param $id
     * @return mixed
     */
    public function getInfoByAdmin($id)
    {
        return $this->scool->table('new_activity_tmp')->where('id', $id)->first();
    }

    /**
     * 审核过的活动的票子信息
     * @param $id
     * @return mixed
     */
    public function getTicketByEventId($id)
    {
        return $this->scool->table('new_activity_ticket')->where('object_id', $id)->get();
    }

    /**
     * 未审核的活动的票子信息
     * @param $id
     * @return mixed
     */
    public function getTicketTmpByEventId($id)
    {
        return $this->scool->table('new_activity_ticket_tmp')->where('object_id', $id)->get();
    }


    /**
     * 下线
     * @param $id
     * @return mixed
     */
	public function doOffline($id)
	{
        //new_all_list表下线
        $this->scool->table('new_all_list')->where('object_id', $id)->where('object_type', 3)->update(['status' => 0]);
        //new_activity表下线
		return $this->scool->table('new_activity')->where('id', $id)->update(['status' => 0]);
	}

    /**
     * 上线
     * @param $id
     * @return mixed
     */
    public function doOnline($id)
    {
        //new_all_list表下线
        $this->scool->table('new_all_list')->where('object_id', $id)->where('object_type', 3)->update(['status' => 1]);
        //new_activity表下线
        return $this->scool->table('new_activity')->where('id', $id)->update(['status' => 1]);
    }

    //删除记录
    public function deletetmp($id)
    {
        return $this->scool->table('new_activity_tmp')->where('id', $id)->delete();
    }

	//文章中的多图
	public function getShareImagesById($id)
	{
		return $this->scool->table('info_share_img')->where('info_share_id', $id)->get();
	}

	//获取机器人
	public function getRobot()
	{
		$rs = $this->scool->table('user')->where('is_robot', 1)->where('is_black', 0)->select('id')->get();

		return $rs;
	}

	//机器人增加喜欢数
	public function robotLike($num, $id)
	{
		$robot = $this->getRobot();

		shuffle($robot);

		$useRobot = array_slice($robot, 0, $num);

		$like = 0;

		$insertArr = [];

		foreach($useRobot as $rel) {

			$rs = $this->scool->table('like')->where('object_id', $id)->where('user_id', $rel->id)->first();

			if(!$rs){

				$insertArr[] = [
								'object_id' 	=> $id, 
								'object_type' 	=> 1, 
								'user_id' 		=> $rel->id, 
								'create_time' 	=> time()
							];
			}

		}
		if($insertArr){
			$rs = $this->scool->table('like')->insert($insertArr);
		}
		
		$countLike = $this->scool->table('like')->where('object_id', $id)->count();

		$this->scool->table('info_share')->where('id', $id)->update(['like' => $countLike]);
	
		return $rs ? true : false;

	}

    /**
     * 写入 new_activity_ticket 表
     * @param $data
     * @return mixed
     */
    public function createTicket($data)
    {
        return DB::connection('scool')->table('new_activity_ticket')->insert($data);
    }

    /**
     * 写入 new_activity_ticket 表
     * @param $data
     * @return mixed
     */
    public function createTicketTmp($data)
    {
        return DB::connection('scool')->table('new_activity_ticket_tmp')->insert($data);
    }

    /**
     * 更新数据
     * @param $data
     * @param $id
     * @return mixed
     */
    public function updateTicket($data, $id)
    {
        $ret = $this->scool->table('new_activity_ticket')->select('id')->where('object_id', $id)->where('title', $data['title'])->get();
        if(empty($ret)){
            return $this->scool->table('new_activity_ticket')->insert($data);
        }
        return $this->scool->table('new_activity_ticket')->where('object_id', $id)->where('title', $data['title'])->update($data);
    }

    /**
     * 删除票子信息
     * @param $data
     * @param $id
     * @return mixed
     */
    public function deleteTicket($data, $id)
    {
        $ret = $this->scool->table('new_activity_ticket')->select('id')->where('object_id', $id)->where('title', $data['title'])->first();
        if(!empty($ret)){
            return $this->scool->table('new_activity_ticket')->where('id', $ret->id)->update(['status' => 0]);
        }
        return true;
    }

    /**
     * 更新数据
     * @param $data
     * @param $id
     * @return mixed
     */
    public function updateTicketTmp($data, $id)
    {
        $ret = $this->scool->table('new_activity_ticket_tmp')->select('id')->where('object_id', $id)->where('title', $data['title'])->get();
        if(empty($ret)){
            return $this->scool->table('new_activity_ticket_tmp')->insert($data);
        }
        return $this->scool->table('new_activity_ticket_tmp')->where('object_id', $id)->where('title', $data['title'])->update($data);
    }

    /**
     * 删除票子信息
     * @param $data
     * @param $id
     * @return mixed
     */
    public function deleteTicketTmp($data, $id)
    {
        $ret = $this->scool->table('new_activity_ticket_tmp')->select('id')->where('object_id', $id)->where('title', $data['title'])->first();
        if(!empty($ret)){
            return $this->scool->table('new_activity_ticket_tmp')->where('id', $ret->id)->update(['status' => 0]);
        }
        return true;
    }

    /**
     * 更新 回退 理由
     * @param $data
     * @return mixed
     */
    public function updateReturnReason($data)
    {
        return $this->scool->table('new_activity_tmp')->where('id', $data['id'])->update(['return_reason' => $data['returnReason']]);
    }

    /**
     * 获取用户关注的用户列表
     * @param $userId
     * @return mixed
     */
    public function getUserFollow($userId)
    {
        $ret = $this->scool->table('follow')
            ->select('user.id', 'user.nickname')
            ->join('user', 'user.id', '=', 'follow.follow_user_id')
            ->where('follow.user_id', $userId)->get();
        return $ret;
    }

}